/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODE_DB_SQL_DELAYTHREAD_H_
#define NODE_DB_SQL_DELAYTHREAD_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include "node/util/lockedqueue.h"
#include "node/system/thread.h"
#include "node/system/mutex.h"

namespace node {
    namespace db {

        class Database;
        class SqlOperation;
        class Connection;

        class SqlDelayThread : public ::node::system::Thread
        {
            typedef ::node::util::LockedQueue<SqlOperation*, ::node::system::Mutex> SqlQueue;

        public:
            SqlDelayThread(Database* db, Connection* conn);
            ~SqlDelayThread();

            bool delay(SqlOperation* sql) { sql_queue_.add(sql); return true; }
            virtual void stop();

        protected:
            virtual void run();

        private:
            void processRequests(); 

            SqlQueue sql_queue_;
            Database* db_;
            Connection* conn_;
            volatile bool running_;
        };

    }
}

#endif // NODE_DB_SQL_DELAYTHREAD_H_
